package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AppArtifact;
import com.ruoyi.system.domain.AppTicket;
import com.ruoyi.system.mapper.AppArtifactMapper;
import com.ruoyi.system.mapper.AppTicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppOrderMapper;
import com.ruoyi.system.domain.AppOrder;
import com.ruoyi.system.service.IAppOrderService;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class AppOrderServiceImpl implements IAppOrderService 
{
    @Autowired
    private AppOrderMapper appOrderMapper;
    @Autowired
    private AppTicketMapper appTicketMapper;

    /**
     * 查询订单信息
     * 
     * @param orderId 订单信息主键
     * @return 订单信息
     */
    @Override
    public AppOrder selectAppOrderByOrderId(Long orderId)
    {
        return appOrderMapper.selectAppOrderByOrderId(orderId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param appOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<AppOrder> selectAppOrderList(AppOrder appOrder)
    {
        return appOrderMapper.selectAppOrderList(appOrder);
    }

    /**
     * 新增订单信息
     * 
     * @param appOrder 订单信息
     * @return 结果
     */
    @Override
    public int insertAppOrder(AppOrder appOrder)
    {
        if (Objects.isNull(SecurityUtils.getLoginAppUser())) {
            throw new ServiceException("请先登录");
        }
        if (Objects.isNull(appOrder.getTicketId())) {
            throw new ServiceException("请选择要购买的票");
        }
        if (Objects.isNull(appOrder.getNum())) {
            throw new ServiceException("请输入购买数量");
        }
        AppTicket appTicket = appTicketMapper.selectAppTicketByTicketId(appOrder.getTicketId());
        if (appTicket.getNum() < appOrder.getNum()) {
            throw new ServiceException("剩余门票不足");
        }
        appTicket.setSoldNum(appTicket.getSoldNum() + appOrder.getNum());
        appTicket.setNum(appTicket.getNum() - appOrder.getNum());
        appTicketMapper.updateAppTicket(appTicket);
        appOrder.setTicketPrice(appTicket.getTicketPrice());
        appOrder.setTicketType(appTicket.getTicketType());
        appOrder.setTicketDate(appTicket.getTicketDate());
        appOrder.setTicketId(appTicket.getTicketId());

        AppUser user = SecurityUtils.getLoginAppUser().getAppUser();
        appOrder.setUserId(user.getUserId());
        appOrder.setUserName(user.getUserName());
        appOrder.setNickName(user.getNickName());
        appOrder.setMobile(user.getMobile());
        appOrder.setEmail(user.getEmail());

        appOrder.setCreateTime(DateUtils.getNowDate());
        return appOrderMapper.insertAppOrder(appOrder);
    }

    /**
     * 修改订单信息
     * 
     * @param appOrder 订单信息
     * @return 结果
     */
    @Override
    public int updateAppOrder(AppOrder appOrder)
    {
        appOrder.setUpdateTime(DateUtils.getNowDate());
        return appOrderMapper.updateAppOrder(appOrder);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteAppOrderByOrderIds(Long[] orderIds)
    {
        return appOrderMapper.deleteAppOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteAppOrderByOrderId(Long orderId)
    {
        return appOrderMapper.deleteAppOrderByOrderId(orderId);
    }
}
